package com.sheng.maildemo.service;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMailMessage;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

/**
 * @author maosir
 * @create 2023-06-09 16:01
 */
@Service
public class MailService {

    @Value("${spring.mail.username}")
    private String from;

    /**
     * 邮件发送类
     */
    @Resource
    private JavaMailSender mailSender;

    public void sayHello() {
        System.out.println("Hello World!");
    }

    /**
     * 发送简单邮件
     * @param to 接收人邮箱地址
     * @param subject 邮件主题
     * @param context 邮件内容
     */
    public void sendSimpleMail(String to, String subject, String context) {
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setTo(to);
        simpleMailMessage.setSubject(subject);
        simpleMailMessage.setText(context);
        simpleMailMessage.setFrom(from);

        //发送邮件
        mailSender.send(simpleMailMessage);
    }

    /**
     * 发送HTML邮件
     * @param to 接收人邮箱地址
     * @param subject 邮件主题
     * @param context 邮件内容
     */
    public void sendHtmlMail(String to, String subject, String context) throws MessagingException {
        MimeMessage message = mailSender.createMimeMessage();

        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(context, true);

        mailSender.send(message);
    }

    /**
     * 发送附件邮件
     * @param to 接收人邮箱地址
     * @param subject 邮件主题
     * @param context 邮件内容
     * @param filePath 附件文件路径
     */
    public void sendAttachmentsMail(String to, String subject, String context, String filePath) throws MessagingException {
        MimeMessage message = mailSender.createMimeMessage();

        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(context, true);

        /*
            给发送邮件添加附件
         */
        FileSystemResource file = new FileSystemResource(new File(filePath));
        String filename = file.getFilename();
        helper.addAttachment(filename, file);
        //helper.addAttachment(filename, file);  //如需添加多个附件，可以多次调用addAttachment()

        mailSender.send(message);
    }

    /**
     * 发送资源邮件
     * @param to 接收人邮箱地址
     * @param subject 邮件主题
     * @param context 邮件内容
     * @param rscPath 资源路径
     * @param rscId 资源id
     * @throws MessagingException
     */
    public void sendInlineMail(String to, String subject, String context, String rscPath, String rscId) throws MessagingException {
        MimeMessage message = mailSender.createMimeMessage();

        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(context, true);

        /*
            给发送邮件添加资源
         */
        FileSystemResource res = new FileSystemResource(new File(rscPath));
        helper.addInline(rscId, res);

        mailSender.send(message);
    }
}
